﻿#--------------------------------------------------------------------------------------------------------------------------------------------------------
#
# Mi-DevEnv			:	http://midevenv.codeplex.com 
#					:	(c) IT Bridge 2012
#					:	A concept originated by Chris Nurse
#
# Licence			:	GNU General Public License version 2 (GPLv2)
#					:	http://midevenv.codeplex.com/license
#
# Summary           :   This is the interface to Windows Hyper-V
#
#--------------------------------------------------------------------------------------------------------------------------------------------------------

Set-StrictMode -Version Latest

if ((Get-Module MDE-Common) -eq $null) {
    Import-Module MDE-Common
    }

Function Get-MachineExists() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName
)

    $vm = Get-VM -Name $MachineName -ErrorAction SilentlyContinue

    if ($vm -eq $null) {
        return $false
        }
        else {
        return $true
        }
    }
    
Function Get-VirtualDrivePath() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName
) 

    return "$VirtualDiskFolder\$MachineName.vhdx"
}

Function New-VirtualDrive() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName,
[Parameter(mandatory=$true)][string]$TemplateName,
[Parameter(mandatory=$true)][switch]$Differencing
) 

    $dskPath = Get-VirtualDrivePath($MachineName)
    $source = "$VMTemplateFolder\$TemplateName.vhdx"

    if ($Differencing -eq $true) {
        Write-Verbose "Create differencing drive $dskPath"
        New-VHD -Path $dskPath -ParentPath $source -Differencing | Out-Null
        }
    else {
        Write-Verbose "Copy template drive $dskPath"
        Copy-Item -Path $source -Destination $dskPath
        }
}

Function Get-VirtualDrive() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName,
[Parameter(mandatory=$true)][switch]$Confirm=$true
) 

    $dskPath = Get-VirtualDrivePath($MachineName) 
    Write-Output (Get-VHD -Path $dskPath)
    }

Function Remove-VirtualDrive() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName,
[Parameter(mandatory=$true)][switch]$Confirm=$true
) 

    $dskPath = Get-VirtualDrivePath($MachineName) 

    if(Test-Path $dskPath) {
        Write-Verbose "Delete virtual drive $dskPath"
        Dismount-VHD –Path $dskPath -ErrorAction SilentlyContinue -Confirm:$Confirm
        Remove-Item -Path $dskPath -Force -Confirm:$Confirm
        }

    if(Test-Path $dskPath) {
        throw "Failed to remove previous drive image"
        }
}

Function Mount-VirtualDrive() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName
) 
    $dskPath = Get-VirtualDrivePath($MachineName)

    Write-Verbose "Select volume"
    $Disk = Mount-VHD -Passthru -Path $dskPath -Confirm:$false
    $Partition = Get-Partition -DiskNumber $Disk.DiskNumber -PartitionNumber 2
    $VirtualDrive = $Partition.DriveLetter
    
    return $VirtualDrive
}

Function Dismount-VirtualDrive() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName
) 

    $dskPath = Get-VirtualDrivePath($MachineName)
    Dismount-VHD -Path $dskPath 
}

Function New-VirtualMachine() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName, 
[Parameter(mandatory=$true)][string]$VirtualNetwork, 
[Parameter(mandatory=$true)][int]$MemoryMB,
[Parameter(mandatory=$false)][switch]$Differencing
) 
    $dskPath = Get-VirtualDrivePath($MachineName)
    New-VM -Name $MachineName -Path "$MachineFolder" -VHDPath "$dskPath" -MemoryStartupBytes ($MemoryMB*1024*1024)  -SwitchName $VirtualNetwork -BootDevice IDE | Out-Null
}

Function Start-VirtualMachine() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName
) 
    Start-VM -Name $MachineName 
}

Function Remove-VirtualMachine() {
[CmdletBinding()]
Param(
[Parameter(mandatory=$true)][string]$MachineName, 
[Parameter(mandatory=$true)][switch]$Confirm=$true
) 

    $vm = Get-VM $MachineName -ErrorAction SilentlyContinue

    if ($vm -ne $null) {
        Stop-VM $vm -Force -TurnOff -WarningAction SilentlyContinue -Confirm:$Confirm
        Remove-VM -Name $MachineName -Force -Confirm:$Confirm
        }
}